package com.lightsh.coverflow;

import java.io.Console;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Transformation;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;


public class CoverFlowGallery extends Gallery {

	private static final boolean DEBUG = true;
	private static final String TAG = "CoverFlow";
	private Camera mCamera = new Camera();
	private int mMaxRotationAngle = 68;
	private int mMaxZoom = 0;
	private int mCoveflowCenter;
	private boolean mAlphaMode = false;
	private boolean mCircleMode = false;

	// 图片在需要旋转角度时，进行z轴移动的比例(IMAGE_SCALL_Z_RANGE * rotateRange)
	private static final float IMAGE_SCALL_Z_RANGE = 4;
	public CoverFlowGallery(Context context) {
		super(context);
		init();
	}

	public CoverFlowGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public CoverFlowGallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	private void init(){
		this.setStaticTransformationsEnabled(true);
		setSpacing(0);
	}
	public int getMaxRotationAngle() {
		return mMaxRotationAngle;
	}

	public void setMaxRotationAngle(int maxRotationAngle) {
		mMaxRotationAngle = maxRotationAngle;
	}

	public boolean getCircleMode() {
		return mCircleMode;
	}

	public void setCircleMode(boolean isCircle) {
		mCircleMode = isCircle;
	}

	public boolean getAlphaMode() {
		return mAlphaMode;
	}

	public void setAlphaMode(boolean isAlpha) {
		mAlphaMode = isAlpha;
	}

	public int getMaxZoom() {
		return mMaxZoom;
	}

	public void setMaxZoom(int maxZoom) {
		mMaxZoom = maxZoom;
	}

	private int getCenterOfCoverflow() {
		return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2
				+ getPaddingLeft();
	}

	private static int getCenterOfView(View view) {
		return view.getLeft() + view.getWidth() / 2;
	}
	
	// 重写Garray的转换方法 ，产生层叠和放大效果
	@Override
	protected boolean getChildStaticTransformation(View child, Transformation t) {
		final int childCenter = getCenterOfView(child);
		final int childWidth = child.getWidth();
		int rotationAngle = 0;
//		t.setTransformationType(Transformation.TYPE_MATRIX);
		if (childCenter == mCoveflowCenter) {
			// 位于中间并且应是处于中间的视图
			transformImageBitmap(child, t, 0, childCenter);
		} else {
			// 计算出旋转的角度
			rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
			if (Math.abs(rotationAngle) > mMaxRotationAngle) {
				rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle
						: mMaxRotationAngle;
			}
			transformImageBitmap(child, t, rotationAngle, childCenter);
//			transformImageBitmap((ImageView) child, t, rotationAngle,
//					(int) Math.floor((mCoveflowCenter - childCenter)/ (childWidth==0?1:childWidth)));
		}
		return true;
	}

	/**
	 * This is called during layout when the size of this view has changed. If
	 * you were just added to the view hierarchy, you're called with the old
	 * values of 0.
	 * 
	 * @param w
	 *            Current width of this view.
	 * @param h
	 *            Current height of this view.
	 * @param oldw
	 *            Old width of this view.
	 * @param oldh
	 *            Old height of this view.
	 */
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		mCoveflowCenter = getCenterOfCoverflow();
		super.onSizeChanged(w, h, oldw, oldh);
	}

	/**
	 * Transform the Image Bitmap by the Angle passed
	 * 
	 * @param imageView
	 *            ImageView the ImageView whose bitmap we want to rotate
	 * @param t
	 *            transformation
	 * @param rotationAngle
	 *            the Angle by which to rotate the Bitmap
	 */
	private void transformImageBitmap(View child, Transformation t, int rotationAngle, int childCenter) {
		mCamera.save();
		t.clear();
		final Matrix imageMatrix = t.getMatrix();
		final int imageHeight = child.getLayoutParams().height;
		final int imageWidth = child.getLayoutParams().width;
		final int rotation = Math.abs(rotationAngle);
		
		// 根据旋转角度放大(缩小)图片
		float zoomAmount = (float) (mMaxZoom + (rotation * IMAGE_SCALL_Z_RANGE));
		mCamera.translate(0.0f, 0.0f, zoomAmount);
		if(mAlphaMode){
			t.setAlpha(1.0f - (float)rotation / (2 * mMaxRotationAngle));
		}
//		if (rotation <= mMaxRotationAngle) {
//			float zoomAmount = -(float) (mMaxZoom + (rotation * 1.5));
//			mCamera.translate(0.0f, 0.0f, zoomAmount);
//			
//			// 这里的y轴移动有意义？
////			if (mCircleMode) {
////				if (rotation < 40)
////					mCamera.translate(0.0f, 155, 0.0f);
////				else
////					mCamera.translate(0.0f, (255 - rotation * 2.5f), 0.0f);
////			}
//			// 使用Transformation的alpha更合适？
//			// 这里alpha的最小值为0.5
//			if(mAlphaMode){
//				t.setAlpha(1.0f - (float)rotation / (2 * mMaxRotationAngle));
//			}
////			if (mAlphaMode) {
////				((ImageView) (child)).setAlpha((int) (255 - rotation * 2.5));
////			}
//		}
		mCamera.rotateY(rotationAngle);
		
		mCamera.getMatrix(imageMatrix);
		
		// 还原比例
		imageMatrix.preScale(1/CoverFlowAdapter.IMAGE_X_SCALL_RANGE, 1);
		
		// FIXME 旋转前移动进行移动,
		// y轴需要移动图片的一半，这样图片才是正确的视角
		
		imageMatrix.preTranslate(-(imageWidth/2), -(imageHeight/2));
		float translateRange = (1 - ((float)mCoveflowCenter - childCenter)/child.getWidth() );
		if(translateRange > 1.5){
			translateRange = 1.5f;
		}else{
			if(translateRange < 0.5){
				translateRange = 0.5f;
			}
		}
		float translateX = (imageWidth / 2) * translateRange;
		imageMatrix.postTranslate(translateX, imageHeight/2);
		mCamera.restore();
	}
}
